package com.door.erp.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.door.erp.dwz.DWZ;
import com.door.erp.dwz.DwzResponse;
import com.door.erp.model.Client;
import com.door.erp.sevice.ClientLoginService;


@Controller
public class LoginController {
	
	private String account;
	
	private String password;
	
	@Resource(name="clientLoginService",type=ClientLoginService.class)
	private ClientLoginService clientLoginService;
	

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}



	@RequestMapping("/login.do")
	public String login() {
		//System.out.println(account);
		return "main";
	}
	
	@RequestMapping("/clientLogin.do")
	public ModelAndView clientLogin(HttpServletRequest request){
		ModelAndView m = new ModelAndView("dwzresponse");
		String phone = request.getParameter("phone");
		String pwd = request.getParameter("password");
		boolean isValidate = clientLoginService.validateClientLogin(phone, pwd);
		DwzResponse response = new DwzResponse();
		if(isValidate){
			Client client = clientLoginService.getClientByAccount(phone, pwd);
			HttpSession session =  request.getSession();
			session.setAttribute("client", client);
			response.setStatusCode(DWZ.statusCode_OK);
			response.setMessage(DWZ.message_OK);
		}else{
			response.setStatusCode(DWZ.statusCode_ERROR);
			response.setMessage(DWZ.message_LOGIN_ERROR);
		}
		m.addObject("response", response);
		return m;
	}
	
	@RequestMapping("/forward.do")
	public String forward(HttpServletRequest request){
		String jspName = request.getParameter("jspName");
		return jspName;
		
	}

}
